package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import one.stand.mapper.AdminRecordMapper;
import one.stand.mapper.VersionMapper;
import one.stand.model.AdminRecordModel;
import one.stand.model.CompanyModel;
import one.stand.model.ResultModel;
import one.stand.model.VersionModel;
import one.stand.request.CompanyRequest;
import one.stand.request.VersionRequest;
import one.stand.response.VersionResponse;
import one.stand.service.CompanyBaseService;
import one.stand.service.CompanyService;
import one.stand.util.DateUtil;
import one.stand.util.MoneyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@Slf4j
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private AdminRecordMapper adminRecordMapper;
    @Autowired
    private VersionMapper versionMapper;

    /**
     * 审核公司
     */
    @Override
    public ResultModel<Boolean> auditCompany(CompanyRequest request) {
        log.info("审核公司参数：{}", JSON.toJSONString(request));
        if (request == null || request.getCompanyId() == null) {
            return ResultModel.checkParamFail();
        }
        int r = companyBaseService.audit(request.getCompanyId(), request.getAuditState(), request.getAuditRemark());
        if (r > 0) {
            // 公司审核成功
//            if (request.getAuditState().equals(AuditStateEnums.APPLY_SUCCESS.getCode())) {
//
//            }
            return ResultModel.success(true);
        }
        return ResultModel.fail();
    }

    @Override
    public ResultModel<Boolean> configureAgen(CompanyRequest request) {
        log.info("设置企业购买版本，代理权限和代理价格：{}", JSON.toJSONString(request));
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        companyBaseService.agent(request.getCompanyId(), request.getAgentMoney(), request.getIsAgent(), request.getCompanyType());
        // 增加操作记录
        adminRecordMapper.insertSelective(new AdminRecordModel() {{
            setAdminId(request.getId().intValue());
            setCompanyId(request.getCompanyId());
            if (request.getIsAgent() == 0) {
                setContent("撤销代理商标示");
            } else {
                setContent("设置存为代理商，代理价格:" + MoneyUtil.getWithUnit(request.getAgentMoney()));
            }
            setOperationId(2);
            setCreateTime(DateUtil.currentDate());
        }});
        return ResultModel.success();
    }

    @Override
    public ResultModel<Map<String, Object>> appletCodeManage(VersionRequest request) {
        log.info("查询小程序代码管理列表参数：{}", JSON.toJSONString(request));
        VersionModel model = new VersionModel();
        if (request != null) {
            BeanUtils.copyProperties(request, model);
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<VersionModel> list = versionMapper.selectList(model);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        PageInfo<CompanyModel> pageInfo = new PageInfo(list);
        Map<String, Object> map = new HashMap<>(1);
        map.put("pageInfo", pageInfo);
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<VersionResponse> appletCodeManageInfo(VersionRequest request) {
        if (request == null || request.getVersionId() == null) {
            return ResultModel.checkParamFail();
        }
        VersionModel model = versionMapper.selectByPrimaryKey(request.getVersionId());
        VersionResponse response = new VersionResponse();
        if (model != null) {
            BeanUtils.copyProperties(model, response);
        }
        return ResultModel.success(response);
    }

    @Override
    public ResultModel<Boolean> addVersionInfo(VersionRequest request) {
        log.info("添加版本号：{}", JSON.toJSONString(request));
        if (request == null || StringUtils.isBlank(request.getAppId()) || StringUtils.isBlank(request.getTemplateId()) || StringUtils.isBlank(request.getVersionNum())) {
            return ResultModel.checkParamFail();
        }
        Integer versionType = versionMapper.selectCompanyAccount(request.getAppId());
        if (versionType == null) {
            return ResultModel.fail("AppId异常，请稍后重试~！");
        }
        if (versionMapper.addVerification(new VersionModel() {{
            setVersionType(versionType);
            setTemplateId(request.getTemplateId());
            setVersionNum(request.getVersionNum());
        }}) > 0) {
            return ResultModel.fail("版本或者模板重复");
        }

        VersionModel model = new VersionModel();
        BeanUtils.copyProperties(request, model);
        model.setCreateTime(DateUtil.currentDate());
        model.setIsDel(0);
        model.setVersionState(1);
        model.setVersionType(versionType);
        versionMapper.insertSelective(model);
        return ResultModel.success(true);
    }

    @Override
    public ResultModel<List<String>> selectAppId(Integer versionId) {
        List<String> list = versionMapper.selectAppId(versionId);
        return ResultModel.success(list);
    }
}
